package net.cloudcodex.shared;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Constants;

/**
 * Action Message action : Speech, Whisp, ...
 * TODO complete possible actions
 */
public enum MessageAction {

	SPEECH("S"), WHISP("W");

	public interface Constant extends Constants {
		public Map<String, String> localize();
	}

	/**
	 * Code.
	 */
	private final String code;

	private Constant constant;

	/**
	 * @return the code.
	 */
	public String getCode() {
		return code;
	}
	
	/**
	 * @param code code.
	 */
	private MessageAction(String code) {
		this.code = code;
	}
	
	/**
	 * Get a MessageAction from a code (ie : from datastore).
	 * @param code code to map.
	 * @return message type.
	 */
	public static MessageAction getMessageAction(String code) {
		if(code == null) {
			return null;
		}
		for(MessageAction action : values()) {
			if(action.getCode().equals(code)) {
				return action;
			}
		}
		return null;
	}

	public String toLocaleString() {
		if (!GWT.isClient()) {
			return toString();
		}
		if (this.constant == null) {
			this.constant = GWT.create(Constant.class);
		}
		return this.constant.localize().get(this.name());
	}
}
